﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Data.Entity;
using OrderIT.Model;

namespace CodeFirstDemo
{
    class Program
    {
           
        static void Main(string[] args)
        {
             OrderITEntities db = new OrderITEntities();
                // Create and save a new Blog

                //    Console.Write("Enter a name for a new Blog: ");
                //    var name = Console.ReadLine();

                //    var blog = new Blog { Name = name };
                //    db.Blogs.Add(blog);
                //    db.SaveChanges();

                //    // Display all Blogs from the database


                //    //wupan     

                //    var query = from b in db.Blogs
                //                orderby b.Name
                //                select b;



                //       //g
                //    Console.WriteLine("All blogs in the database:");
                //    foreach (var item in query)
                //    {
                //        Console.WriteLine(item.Name);
                //    }

                //    Console.WriteLine("Press any key to exit...");
                //    Console.ReadKey();
                //}

                
                var orders4 = db.Order.Include("Customer");
                var sql = (orders4 as ObjectQuery).ToTraceString();

            Console.WriteLine(sql);
                var orders1 = from o in db.Order
                              where o.OrderDetails
                                     .Select(d => d.Product.ProductId)
                                     .Distinct()
                                     .Count() > 2
                              select o;
                sql = (orders1 as ObjectQuery).ToTraceString();
               Console.WriteLine(sql);
                var orders2 = from o in db.Order
                              where o.OrderDetails.Any(d => d.Product.Brand == "MyBrand")
                              select o;

             sql = (orders2 as ObjectQuery).ToTraceString();
               Console.WriteLine(sql);
            
             Console.ReadKey();

        }
    }
    public class Blog
    {
        public int BlogId { get; set; }
        public string Name { get; set; }

        public virtual List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public int BlogId { get; set; }
        public virtual Blog Blog { get; set; }
    }

    public class BlogContext : DbContext 
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    }
}
